package com.settlement.system.controller;

import com.alibaba.excel.context.AnalysisContext;
import com.settlement.system.common.enums.ReturnStatusEnum;
import com.settlement.system.common.util.common.FasUtil;
import com.settlement.system.model.entity.common.TblOrderCloseAccount;
import com.settlement.system.model.entity.common.TblReceiveRefundAccount;
import com.settlement.system.model.entity.common.TblReceiveRefundShop;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.reqparam.OrderCloseAccountReqParam;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblOrderCloseAccountService;
import com.settlement.system.service.ITblReceiveRefundAccountService;
import com.settlement.system.service.ITblReceiveRefundShopService;
import io.swagger.annotations.Api;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 订单稽核-关账表 前端控制器
 *
 * @author Ximi
 * @since 2020-09-17
 */

@Api(tags = "订单稽核-关账表", description = "订单稽核-关账表")
@RestController
@Slf4j
@RequestMapping("/order/close/account")
public class TblOrderCloseAccountController extends ExcelController<TblOrderCloseAccount> {

    @Resource
    private ITblOrderCloseAccountService tblOrderCloseAccountService;

    @Resource
    private ITblReceiveRefundShopService tblReceiveRefundShopService;

    @Resource
    private ITblReceiveRefundAccountService tblReceiveRefundAccountService;

    /*@Reference(version = "${dubbo.acs.api.version}", timeout = 6000)
    private OrderAuditCloseAccountApi auditCloseAccountApi;*/

    @Override
    public IBaseService init() {
        return tblOrderCloseAccountService;
    }

    @Override
    protected ObjectDataResponse insert(@RequestBody TblOrderCloseAccount model, HttpServletRequest request) {
        try {
            setDefualt(model);
            String error = verifyInsert(model);
            if (!StringUtils.isEmpty(error)) {
                return new ObjectDataResponse(ReturnStatusEnum.CHECK_FAILURE.getValue(), error);
            }
            tblOrderCloseAccountService.insert(model);
            return new ObjectDataResponse(200, "新增成功");
        } catch (Exception ex) {
            return new ObjectDataResponse(500, ex.getMessage());
        }
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        FasUtil.formatInQueryCondition(model, "enterAccountBody");
    }

    /**
     * 验证新增的数据
     *
     * @param account
     * @return
     */
    private String verifyInsert(TblOrderCloseAccount account) {

        if (StringUtils.isEmpty(account.getThreeLevelChannelNo())
                && StringUtils.isEmpty(account.getShopAccount())) {
            return "三级来源和店铺账号不能同时为空";
        }

        if (!StringUtils.isEmpty(account.getThreeLevelChannelNo())
                && !StringUtils.isEmpty(account.getShopAccount())) {
            return "三级来源和店铺账号只能二选一";
        }

        try {
            if (!StringUtils.isEmpty(account.getThreeLevelChannelNo())) {
                TblReceiveRefundShop refundShop = tblReceiveRefundShopService.getByThreeChannelNo(account.getThreeLevelChannelNo());
                if (refundShop == null) {
                    return "三级来源不存在";
                }
                account.setChannelNo(refundShop.getChannelNo());
                account.setEnterAccountBody(refundShop.getNcEnterAccountBody());
                account.setChannelName(refundShop.getChannelName());
                account.setThreeLevelChannelName(refundShop.getThreeChannelName());
                account.setThreeLevelChannelNo(refundShop.getThreeChannelNo());
            }

            if (!StringUtils.isEmpty(account.getShopAccount())) {
                TblReceiveRefundAccount refundAccount = tblReceiveRefundAccountService.getOrByShopAccount(account.getShopAccount());
                if (refundAccount == null) {
                    return "店铺账号不存在";
                }
                account.setChannelNo(refundAccount.getChannelNo());
                account.setChannelName(refundAccount.getChannelName());
                account.setEnterAccountBody(refundAccount.getNcEnterAccountBody());
            }

        } catch (Exception ex) {
            log.error("验证稽核锁帐异常", ex);
            return "系统异常";
        }

        if (account.getInitialDate() == null) {
            return "初始日期不能为空";
        }

        if (account.getRealCloseAccountDate() == null) {
            return "锁帐日期不能为空";
        }

        if (account.getInitialDate().compareTo(account.getRealCloseAccountDate()) > 0) {
            return "锁帐日期不能小于初始日期";
        }

        account.setNeedRollbackAccountData(account.getInitialDate());
        account.setValidCloseAccountDate(account.getInitialDate());
        return null;
    }

    @PostMapping("closeAccount")
    public ObjectDataResponse closeAccount(@RequestBody OrderCloseAccountReqParam reqParam) {
        try {
            if (CollectionUtils.isEmpty(reqParam.getIds())) {
                return new ObjectDataResponse(500, "请选择需要锁帐的数据");
            }

            if (reqParam.getRealCloseAccountDate() == null) {
                return new ObjectDataResponse(500, "请选择需要锁帐的日期");
            }

            tblOrderCloseAccountService.closeAccount(reqParam.getIds(), reqParam.getRealCloseAccountDate());

            try {
                for (String id : reqParam.getIds()) {
                    /*auditCloseAccountApi.closeOrderAccount(id);*/
                }
            } catch (Exception ex) {
                log.error("dubbo closeOrderAccount exception", ex);
            }

        } catch (Exception ex) {
            log.error("锁帐异常", ex);
            new ObjectDataResponse(500, ex.getMessage());
        }
        return new ObjectDataResponse(200, "更新成功");
    }

    @Override
    public Class<?> getModelClass() {
        return TblOrderCloseAccount.class;
    }

    @Override
    protected ObjectDataResponse delete(@RequestBody TblOrderCloseAccount model, HttpServletRequest request) {
        try {
            TblOrderCloseAccount account = tblOrderCloseAccountService.getById(model.getId());
            if (account.getInitialDate().compareTo(account.getRealCloseAccountDate()) != 0) {
                return new ObjectDataResponse(ReturnStatusEnum.CHECK_FAILURE.getValue(), "关账日期不等于初始日期不允许删除");
            }
            tblOrderCloseAccountService.deleteCloseAccount(account);
            return new ObjectDataResponse(ReturnStatusEnum.REQUEST_SUCCESS);
        } catch (Exception ex) {
            log.error("删除异常:", ex);
            return new ObjectDataResponse(ReturnStatusEnum.REQUEST_ABNORMAL);
        }
    }

    @Override
    public String verifyImportObject(TblOrderCloseAccount account, AnalysisContext analysisContext) {
        return verifyInsert(account);
    }
}
